<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Zhideai\Fieldencrypt\Encrypt;
use Zhideai\Fieldencrypt\Decrypt;
use Rtgm\sm\RtSm4;
use Zhideai\Fieldencrypt\Sign;

class FieldEncryptController extends Controller
{
    //
    const SECRET_KEY="12345678ABCDEFGH";
    private $encObj;
    private $decObj;
    private $sm4Obj;
    public function __construct(){
        $this->encObj=new Encrypt(self::SECRET_KEY);
        $this->decObj=new Decrypt(self::SECRET_KEY);
        $this->sm4Obj=new RtSm4(self::SECRET_KEY);
    }
    public function encStr(Request $request){
        $data=$request['data']??null;
        if(empty($data))
        return $data;
        $text=$data['s']??null;
        if(empty($text))
        return $text;
        $method=$data['t']??'gb';
        if($method=='gb')
        return $this->sm4Obj->encrypt($text,'sm4-ecb');
        return $this->encObj->enc($text);
    }
    public function decStr(Request $request){
        $data=$request['data']??null;
        if(empty($data))
        return $data;
        $str=$data['s']??null;
        if(empty($str))
        return $str;
        $method=$data['t']??'gb';
        if($method=='gb')
        return $this->sm4Obj->decrypt($str,'sm4-ecb','','hex');
        return $this->decObj->dec($str);
    }
    public function sign(Request $req){
        $data=$req['body']??[];
        $signature=Sign::make(ksort($data));
        return $signature;
    }
    public function testEncInsert(Request $request){
        $data=$request['data']??['1','1','1'];
        $db=DB::connection("mysql_enc");
        $insert=$db->insert(
            "insert into tbl_users(acc,pwd,mobile) values(?,?,?)",
            $data
        );
        return response()->json(
            [
                'msg'=>$insert===true?'success':'fail',
            ]
            );
    }
    public function testEncUpdate(Request $request){
        $id=$request['id']??1;
        $acc=$request['acc']??1;
        $pwd=$request['pwd']??1;
        $mobile=$request['mobile']??1;
        $db=DB::connection("mysql_enc");
        $list=DB::select("select id,pwd from tbl_users");
        $v=[];
        foreach($list as $k=>$v1){
         $v[]=$db->update(
            "update tbl_users set pwd=? where id=?",
            [$v1->pwd,$v1->id]
         );
        }
        return response()->json(
            [
                'msg'=>'success',
                'data'=>$v
            ]
            );
    }
    public function testEncSelect(Request $request){
        $data=$request['data']??['1'];
        $db=DB::connection("mysql_enc");
        $data=$db->select(
            "select * from tbl_users where mobile=?",
            $data
        );
        return response()->json(
            [
                'msg'=>'success',
                'data'=>$data
            ]
            );
    }
    private function readPriKey(){
        $root=dirname(app_path());
        $priKeyFile="api_private_key.pem";
        $priKeyPath=$root.'/'.$priKeyFile;
        $f=fopen($priKeyPath,'r');
        $key="";
        while(!feof($f)){
            $key.=fgets($f);
        }
        fclose($f);
        return $key;
    }
}
